// pages/components/poster/index.js
import { qrcode } from "../../assets/js/code";

const { wxml, style } = require("./demo.js");

Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    qrcodeUrl: "", // 二维码图片地址
    src: "",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onShow() {
      this.creatQrCode();
    },
    /**
     * 生成二维码
     */
    creatQrCode() {
      qrcode("myCanvas", "https://via.placeholder.com/600x600", 600, 600);
    },
    /**
     * 点击去支付按钮
     */
    handleClickToPay() {
      const that = this;
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: 300,
        height: 300,
        destWidth: 300,
        destHeight: 300,
        canvasId: "myCanvas",
        success(res) {
          that.setData({
            qrcodeUrl: res.tempFilePath,
          });
        },
        fail(error) {
          console.error("error: ", error);
        },
      });
    },
    onLoad() {
      this.widget = this.selectComponent(".widget");
    },
    renderToCanvas() {
      const p1 = this.widget.renderToCanvas({ wxml, style });
      p1.then((res) => {
        console.log("container", res.layoutBox);
        this.container = res;
      });
    },
    extraImage() {
      const p2 = this.widget.canvasToTempFilePath();
      p2.then((res) => {
        this.setData({
          src: res.tempFilePath,
          width: this.container.layoutBox.width,
          height: this.container.layoutBox.height,
        });
      });
    },
  },
});
